<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="进行产品开发的时候，逻辑的严谨性非常重要，如果一个产品或者程序逻辑上不严谨，就有可能出现功能上的错误。比如我们 15.3.4 节里的这个程序，我们再回顾一下，当单片机定时器时间到了 200ms 后，我们连续把 DS1302 的时间参数的 7 个字节读了出来。但是不管怎么读，都会有一个时间差，在极端的情况下就会出现这样一种情况：假如我们当前的时间是 00:00:59，我们先读秒，读到的秒是 59，然">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="15.7、S1302的BURST模式">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu103/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="进行产品开发的时候，逻辑的严谨性非常重要，如果一个产品或者程序逻辑上不严谨，就有可能出现功能上的错误。比如我们 15.3.4 节里的这个程序，我们再回顾一下，当单片机定时器时间到了 200ms 后，我们连续把 DS1302 的时间参数的 7 个字节读了出来。但是不管怎么读，都会有一个时间差，在极端的情况下就会出现这样一种情况：假如我们当前的时间是 00:00:59，我们先读秒，读到的秒是 59，然">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-31T13:14:53.361Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="15.7、S1302的BURST模式">
<meta name="twitter:description" content="进行产品开发的时候，逻辑的严谨性非常重要，如果一个产品或者程序逻辑上不严谨，就有可能出现功能上的错误。比如我们 15.3.4 节里的这个程序，我们再回顾一下，当单片机定时器时间到了 200ms 后，我们连续把 DS1302 的时间参数的 7 个字节读了出来。但是不管怎么读，都会有一个时间差，在极端的情况下就会出现这样一种情况：假如我们当前的时间是 00:00:59，我们先读秒，读到的秒是 59，然">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu103/">





  <title>15.7、S1302的BURST模式 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu103/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">15.7、S1302的BURST模式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:41:03+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>进行产品开发的时候，逻辑的严谨性非常重要，如果一个产品或者程序逻辑上不严谨，就有可能出现功能上的错误。比如我们 15.3.4 节里的这个程序，我们再回顾一下，当单片机定时器时间到了 200ms 后，我们连续把 DS1302 的时间参数的 7 个字节读了出来。但是不管怎么读，都会有一个时间差，在极端的情况下就会出现这样一种情况：假如我们当前的时间是 00:00:59，我们先读秒，读到的秒是 59，然后再去读分钟，而就在读完秒到还未开始读分钟的这段时间内，刚好时间进位了，变成了 00:01:00 这个时间，我们读到的分钟就是 01，显示在液晶上就会出现一个 00:01:59，这个时间很明显是错误的。出现这个问题的概率极小，但却是实实在在可能存在的。</p>
<p>为了解决这个问题，芯片厂家肯定要给我们提供一种解决方案，这就是 DS1302 的突发模式。突发模式也分为 RAM 突发模式和时钟突发模式，RAM 部分我们不讲，我们只看和时钟相关的 clock burst mode。</p>
<p>当我们写指令到 DS1302 的时候，只要我们将要写的 5 位地址全部写 1，即读操作用 0xBF，写操作用 0xBE，这样的指令送给 DS1302 之后，它就会自动识别出来是 burst 模式，马上把所有的 8 个字节同时锁存到另外的 8 个字节的寄存器缓冲区内，这样时钟继续走，而我们读数据是从另外一个缓冲区内读取的。同样的道理，如果我们用 burst 模式写数据，那么我们也是先写到这个缓冲区内，最终 DS1302 会把这个缓冲区内的数据一次性送到它的时钟寄存器内。</p>
<p>要注意的是，不管是读还是写，只要使用时钟的 burst 模式，则必须一次性读写 8 个寄存器，要把时钟的寄存器完全读出来或者完全写进去。</p>
<p>下边就提供一个 burst 模式的例程给大家学习一下，程序的功能还是与上一节一样的。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br></pre></td><td class="code"><pre><span class="line">/***************************Lcd1602.c 文件程序源代码*****************************/</span><br><span class="line">（此处省略，可参考之前章节的代码）</span><br><span class="line">纯文本复制</span><br><span class="line">/*****************************main.c 文件程序源代码******************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit DS1302_CE = P1^7;</span><br><span class="line">sbit DS1302_CK = P3^5;</span><br><span class="line">sbit DS1302_IO = P3^4;</span><br><span class="line">bit flag200ms = 0; //200ms 定时标志</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">void InitDS1302();</span><br><span class="line">void DS1302BurstRead(unsigned char *dat);</span><br><span class="line">extern void InitLcd1602();</span><br><span class="line">extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);</span><br><span class="line">void main()&#123;</span><br><span class="line">    unsigned char psec=0xAA; //秒备份，初值 AA 确保首次读取时间后会刷新显示</span><br><span class="line">    unsigned char time[8]; //当前时间数组</span><br><span class="line">    unsigned char str[12]; //字符串转换缓冲区</span><br><span class="line">   </span><br><span class="line">    EA = 1; //开总中断</span><br><span class="line">    ConfigTimer0(1); //T0 定时 1ms</span><br><span class="line">    InitDS1302(); //初始化实时时钟</span><br><span class="line">    InitLcd1602(); //初始化液晶</span><br><span class="line">   </span><br><span class="line">    while (1)&#123;</span><br><span class="line">        if (flag200ms)&#123; //每 200ms 读取依次时间</span><br><span class="line">            flag200ms = 0;</span><br><span class="line">            DS1302BurstRead(time); //读取 DS1302 当前时间</span><br><span class="line">           </span><br><span class="line">            if (psec != time[0])&#123; //检测到时间有变化时刷新显示</span><br><span class="line">                str[0] = &apos;2&apos;; //添加年份的高 2 位：20</span><br><span class="line">                str[1] = &apos;0&apos;;</span><br><span class="line">                str[2] = (time[6] &gt;&gt; 4) + &apos;0&apos;; //“年”高位数字转换为 ASCII 码</span><br><span class="line">                str[3] = (time[6]&amp;0x0F) + &apos;0&apos;; //“年”低位数字转换为 ASCII 码</span><br><span class="line">                str[4] = &apos;-&apos;; //添加日期分隔符</span><br><span class="line">                str[5] = (time[4] &gt;&gt; 4) + &apos;0&apos;; //“月”</span><br><span class="line">                str[6] = (time[4]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[7] = &apos;-&apos;;</span><br><span class="line">                str[8] = (time[3] &gt;&gt; 4) + &apos;0&apos;; //“日”</span><br><span class="line">                str[9] = (time[3]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[10] = &apos;\0&apos;;</span><br><span class="line">                LcdShowStr(0, 0, str); //显示到液晶的第一行</span><br><span class="line">               </span><br><span class="line">                str[0] = (time[5]&amp;0x0F) + &apos;0&apos;; //“星期”</span><br><span class="line">                str[1] = &apos;\0&apos;;</span><br><span class="line">                LcdShowStr(11, 0, &quot;week&quot;);</span><br><span class="line">                LcdShowStr(15, 0, str); //显示到液晶的第一行</span><br><span class="line">               </span><br><span class="line">                str[0] = (time[2] &gt;&gt; 4) + &apos;0&apos;; //“时”</span><br><span class="line">                str[1] = (time[2]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[2] = &apos;:&apos;; //添加时间分隔符</span><br><span class="line">                str[3] = (time[1] &gt;&gt; 4) + &apos;0&apos;; //“分”</span><br><span class="line">                str[4] = (time[1]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[5] = &apos;:&apos;;</span><br><span class="line">                str[6] = (time[0] &gt;&gt; 4) + &apos;0&apos;; //“秒”</span><br><span class="line">                str[7] = (time[0]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[8] = &apos;\0&apos;;</span><br><span class="line">                LcdShowStr(4, 1, str); //显示到液晶的第二行</span><br><span class="line">               </span><br><span class="line">                psec = time[0]; //用当前值更新上次秒数</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 发送一个字节到 DS1302 通信总线上 */</span><br><span class="line">void DS1302ByteWrite(unsigned char dat)&#123;</span><br><span class="line">    unsigned char mask;</span><br><span class="line">    for (mask=0x01; mask!=0; mask&lt;&lt;=1)&#123; //低位在前，逐位移出</span><br><span class="line">        if ((mask&amp;dat) != 0)&#123; //首先输出该位数据</span><br><span class="line">            DS1302_IO = 1;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            DS1302_IO = 0;</span><br><span class="line">        &#125;</span><br><span class="line">        DS1302_CK = 1; //然后拉高时钟</span><br><span class="line">        DS1302_CK = 0; //再拉低时钟，完成一个位的操作</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_IO = 1; //最后确保释放 IO 引脚</span><br><span class="line">&#125;</span><br><span class="line">/* 由 DS1302 通信总线上读取一个字节 */</span><br><span class="line">unsigned char DS1302ByteRead()&#123;</span><br><span class="line">    unsigned char mask;</span><br><span class="line">    unsigned char dat = 0;</span><br><span class="line">   </span><br><span class="line">    for (mask=0x01; mask!=0; mask&lt;&lt;=1)&#123; //低位在前，逐位读取</span><br><span class="line">        if (DS1302_IO != 0)&#123; //首先读取此时的 IO 引脚，并设置 dat 中的对应位</span><br><span class="line">            dat |= mask;</span><br><span class="line">        &#125;</span><br><span class="line">        DS1302_CK = 1; //然后拉高时钟</span><br><span class="line">        DS1302_CK = 0; //再拉低时钟，完成一个位的操作</span><br><span class="line">    &#125;</span><br><span class="line">    return dat; //最后返回读到的字节数据</span><br><span class="line">&#125;</span><br><span class="line">/* 用单次写操作向某一寄存器写入一个字节，reg-寄存器地址，dat-待写入字节 */</span><br><span class="line">void DS1302SingleWrite(unsigned char reg, unsigned char dat)&#123;</span><br><span class="line">    DS1302_CE = 1; //使能片选信号</span><br><span class="line">    DS1302ByteWrite((reg&lt;&lt;1)|0x80); //发送写寄存器指令</span><br><span class="line">    DS1302ByteWrite(dat); //写入字节数据</span><br><span class="line">    DS1302_CE = 0; //除能片选信号</span><br><span class="line">&#125;</span><br><span class="line">/* 用单次读操作从某一寄存器读取一个字节，reg-寄存器地址，返回值-读到的字节 */</span><br><span class="line">unsigned char DS1302SingleRead(unsigned char reg)&#123;</span><br><span class="line">    unsigned char dat;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 1; //使能片选信号</span><br><span class="line">    DS1302ByteWrite((reg&lt;&lt;1)|0x81); //发送读寄存器指令</span><br><span class="line">    dat = DS1302ByteRead(); //读取字节数据</span><br><span class="line">    DS1302_CE = 0; //除能片选信号</span><br><span class="line">    return dat;</span><br><span class="line">&#125;</span><br><span class="line">/* 用突发模式连续写入 8 个寄存器数据，dat-待写入数据指针 */</span><br><span class="line">void DS1302BurstWrite(unsigned char *dat)&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 1;</span><br><span class="line">    DS1302ByteWrite(0xBE); //发送突发写寄存器指令</span><br><span class="line">   </span><br><span class="line">    for (i=0; i&lt;8; i++)&#123; //连续写入 8 字节数据</span><br><span class="line">        DS1302ByteWrite(dat[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_CE = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 用突发模式连续读取 8 个寄存器的数据，dat-读取数据的接收指针 */</span><br><span class="line">void DS1302BurstRead(unsigned char *dat)&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 1;</span><br><span class="line">    DS1302ByteWrite(0xBF); //发送突发读寄存器指令</span><br><span class="line">    for (i=0; i&lt;8; i++)&#123; //连续读取 8 个字节</span><br><span class="line">        dat[i] = DS1302ByteRead();</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_CE = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* DS1302 初始化，如发生掉电则重新设置初始时间 */</span><br><span class="line">void InitDS1302()&#123;</span><br><span class="line">    unsigned char dat;</span><br><span class="line">    unsigned char code InitTime[] = &#123; //2013 年 10 月 8 日 星期二 12:30:00</span><br><span class="line">        0x00,0x30,0x12, 0x08, 0x10, 0x02, 0x13</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 0; //初始化 DS1302 通信引脚</span><br><span class="line">    DS1302_CK = 0;</span><br><span class="line">    dat = DS1302SingleRead(0); //读取秒寄存器</span><br><span class="line">   </span><br><span class="line">    if ((dat &amp; 0x80) != 0)&#123; //由秒寄存器最高位 CH 的值判断 DS1302 是否已停止</span><br><span class="line">        DS1302SingleWrite(7, 0x00); //撤销写保护以允许写入数据</span><br><span class="line">        DS1302BurstWrite(InitTime); //设置 DS1302 为默认的初始时间</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">   </span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 12; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，执行 200ms 定时 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    static unsigned char tmr200ms = 0;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    tmr200ms++;</span><br><span class="line">    if (tmr200ms &gt;= 200)&#123; //定时 200ms</span><br><span class="line">        tmr200ms = 0;</span><br><span class="line">        flag200ms = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu102/" rel="next" title="15.6、DS1302通信时序介绍">
                <i class="fa fa-chevron-left"></i> 15.6、DS1302通信时序介绍
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu104/" rel="prev" title="15.8、C语言复合数据类型">
                15.8、C语言复合数据类型 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
